package com.bupt.liutong.admin.sch.bo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;

import com.bupt.liutong.admin.core.base.BaseBo;
import com.bupt.liutong.admin.core.base.FileUploader;
import com.bupt.liutong.admin.sch.dao.AlbumPhotoDao;
import com.bupt.liutong.admin.sch.dto.AlbumPhoto;
import com.bupt.liutong.admin.sch.form.AlbumPhotoForm;
import com.bupt.liutong.util.BeanUtils;

public class AlbumPhotoBo extends BaseBo {

	private AlbumPhotoDao albumPhotoDao;
	private FileUploader fileUploader;

	public List<AlbumPhotoForm> getAlbumPhotosByPage(AlbumPhotoForm albumPhotoForm,
			HttpServletRequest request) {
		AlbumPhoto albumPhoto = new AlbumPhoto();
		sm.pagingWithNoCriteria(albumPhotoForm, albumPhoto, "sch_album_photo.getAlbumPhotosCount");
		List<AlbumPhoto> albumPhotos = albumPhotoDao.getAlbumPhotosByPage(albumPhoto);
		return BeanUtils.dtoToForm(albumPhotos, AlbumPhotoForm.class);
	}

	public AlbumPhotoForm getAlbumPhotoById(AlbumPhotoForm albumPhotoForm) {
		AlbumPhotoForm resultForm = new AlbumPhotoForm();
		AlbumPhoto albumPhoto = albumPhotoDao.getAlbumPhotoById(albumPhotoForm.getId());
		BeanUtils.copyProperties(resultForm, albumPhoto);
		return resultForm;
	}

	@Transactional
	public void insertPhoto(AlbumPhotoForm albumPhotoForm, HttpServletRequest request) {
		fileUploader.uploadImages(albumPhotoForm, albumPhotoDao, request);
	}

	@Transactional
	public void updateAlbumPhoto(AlbumPhotoForm albumPhotoForm) {
		AlbumPhoto albumPhoto = new AlbumPhoto();
		BeanUtils.copyProperties(albumPhoto, albumPhotoForm);
		albumPhotoDao.updatePhoto(albumPhoto);
	}

	@Transactional
	public void deleteAlbumPhotos(String[] ids, HttpServletRequest request) {
		if (ids != null) {
			for (int i = 0; i < ids.length; i++) {
				fileUploader.deleteImage(Integer.valueOf(ids[i]), albumPhotoDao, request);
			}
		}
	}

	public void setAlbumPhotoDao(AlbumPhotoDao albumPhotoDao) {
		this.albumPhotoDao = albumPhotoDao;
	}

	public void setFileUploader(FileUploader fileUploader) {
		this.fileUploader = fileUploader;
	}
}
